package bo;

import java.sql.SQLException;
import java.util.List;

import dao.DAOObject;
import exception.BoException;
import exception.DaoException;

public class NegocioBO implements Negocio{
	
	DAOObject dao;

	public Object load(Object aCriteria) throws BoException {
		try {			
			abrirConexion();
			DAOObject tmpDao = getDao();
			Object resultado = tmpDao.load(aCriteria);
			return resultado;
		} catch (DaoException aDaoException) {
			throw new BoException(aDaoException.getMessage());
		} finally {
			cerrarConexion();
		}
	}
	
	public List<Object> getCollection(Object aCriteria) throws BoException {
		try {
			abrirConexion();
			DAOObject tmpDao = getDao();
			List<Object> resultado = tmpDao.getCollection(aCriteria);
			return resultado;
		} catch (DaoException aDaoException) {
			throw new BoException(aDaoException.getMessage());
		} finally {
			cerrarConexion();
		}
	}
		
	/**
	 * Metodo encargado de cerrar la conexion
	 * @throws DaoException Posible excepcion a lanzar
	 */
	public void cerrarConexion() throws BoException {
		try {			
			if (dao.getConexion()!=null && !dao.getConexion().isClosed())
				dao.getConexion().close();
		} catch (SQLException aSqlException) {
			throw new BoException(aSqlException.getMessage());
		}
	}
	
	public DAOObject getDao(){
		return dao;
	}

	public void abrirConexion() throws BoException {		
	}


}
